/*
 * Cache.hpp
 *
 *  Created on: Sep 29, 2010
 *      Author: nicoabie
 */

#ifndef CACHE_HPP_
#define CACHE_HPP_


#include <map>
#include <utility>
#include <stdlib.h>
#include <iostream>
#include "WeakPointer.hpp"
#include "SmartPointer.hpp"
#include "SingletonHolder.hpp"

using namespace std;

template <class T, class Storage>
class Cache {
public:
	typedef SingletonHolder<Cache> CacheType;
	typedef SmartPointer<T,CacheType> SmartPtr_t;
	typedef WeakPointer<T,CacheType> WeakPtr_t;
	typedef multimap<long,SmartPtr_t> Buffer_t;
	typedef multimap<int,long> Count_t;
	typedef typename container_type::iterator iterator;

private:
	Buffer_t container;
	long length;
	Storage storage;
	Count_t times;

public:
	Cache(long l);
	~Cache();
	iterator begin();
	iterator end();
	size_type size();
	bool empty();
	SmartPtr_t getPointer(long id);
	void insert(long id, SmartPtr_t ptr);
	SmartPtr_t createNew(PageType);
	void deleteObject(long id);
	long getRootID();
	bool isRoot(SmartPtr_t root);
	SmartPtr_t getRoot();
	void setRoot(SmartPtr_t root);
	bool hasRoot();
	void erase(iterator pos);
	void clear();
	Storage* getStorage();
	void setStorage(Storage* storage);
};

#endif /* CACHE_HPP_ */
